/*
 * This is a class to convert sudoku raw data into SQL quaries 
 * to input into gamesets table
 * filename, difficulty -> filename.sql 
 * 
 * Usage
 * =====
 * java convertToSql EASY.txt easy
 * 
 * EASY.txt.sql will be created in the same directory
 */
import java.io.BufferedReader;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.File;

public class convertToSql {
	private static void convert(String fileName,String difficulty) {
		String unsolved = "";
		String solved = "";
		String output = "";
		try {
			File originFile = new File(fileName);
			File destinationFile = new File(fileName + ".sql");
			if (!originFile.exists() || destinationFile.exists()) {
				return;
			}
			FileReader reader = new FileReader(originFile);
			BufferedReader in = new BufferedReader(reader);
			FileOutputStream out = new FileOutputStream(destinationFile);
			String string;
			while ((string = in.readLine()) != null) {
				unsolved = string;
				solved = in.readLine();
				if (solved == null){break;}
				output = "INSERT INTO `sudoku`.`gamesets` " +
						"(`id` , `unsolved` , `solved`, `difficulty`) " +
						"VALUES (NULL, '" + unsolved + "', '" + solved + "', '" + difficulty + "');";				
				out.write(output.getBytes());
			}
			in.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

	public static void main(String[] args) {
		if (args.length != 2) {
			System.err.println("usage: java TextReader "
					+ "file location");
			System.exit(0);
		}
		convert(args[0],args[1]);
	}
}
